package com.epro.dx.task;

import android.content.Context;
import android.os.AsyncTask;

import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.OnekeyFilterLocalUtil;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.SearchModel;

/**
 * @author tony
 *         获取主页小banner商品的任务
 */
public class GetHomeBannerAsyncTask extends AsyncTask<Void, Void, String> {

    private MyAsyncTaskListenner myAsyncTaskListenner;
    private int start;
    private SearchModel searchModel;
    private String type;
    private String orderByField;
    private int pageSize;
    private Context mContext;
    private String categoryId;
    private boolean isFilter;

    public GetHomeBannerAsyncTask(Context mContext, int start, int pageSize, boolean isFilter, int categoryId, String type, String orderByField, MyAsyncTaskListenner myAsyncTaskListenner) {
        this.mContext = mContext;
        this.start = start;
        this.pageSize = pageSize;
        this.type = type;
        this.orderByField = orderByField;
        this.myAsyncTaskListenner = myAsyncTaskListenner;
        this.isFilter = isFilter;

        if (categoryId == -2) {
            this.categoryId = OnekeyFilterLocalUtil.getLocalCategoryIds(mContext);
        } else if (categoryId == -3) {
            this.categoryId = OnekeyFilterLocalUtil.getCategoryIds();
        } else {
            this.categoryId = String.valueOf(categoryId);
        }

    }

    @Override
    protected void onPreExecute() {
        myAsyncTaskListenner.onPreTask();
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            searchModel = ProviderFactory.createProductProvider()
                    .getHomeBtnDataList(ActivityUtil.getCustomerID(), isFilter,
                            categoryId, start, pageSize,
                            type, orderByField);
        } catch (DxException e) {
            return e.getErrCode();
        } catch (Exception e) {
            return e.getMessage();
        }
        return "0";
    }

    @Override
    protected void onPostExecute(String result) {
        myAsyncTaskListenner.onPostTask(result, searchModel);
    }

}
